package com.yyh.service.impl;


import com.yyh.domain.Goods;
import com.yyh.service.GoodsService;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Scanner;

/*
     商品接口实现类
 */
public class GoodsServideImpl implements GoodsService {
    public static final Scanner INPUT = new Scanner(System.in);
    public static final LinkedList<Goods> goodsList = new LinkedList<>();
    public static final LinkedList<Goods> carList = new LinkedList<>();
    static {
        goodsList.add(new Goods("10101","海尔冰箱",new BigDecimal("3999.9"),50));
        goodsList.add(new Goods("10102","隔离冰箱",new BigDecimal("2888.8"),30));
        goodsList.add(new Goods("10103","TCL冰箱",new BigDecimal("1777.7"),500));
        goodsList.add(new Goods("10104","美的冰箱",new BigDecimal("3099.9"),20000));
        goodsList.add(new Goods("10105","海尔空调",new BigDecimal("2000.4"),450));
    }
    /**
     * 购买商品的功能
     */
    @Override
    public void shopping() {
        outer:
        while (true) {
            // 1. 展示所有的商品信息
            System.out.println("商品编号\t\t商品名称\t\t商品价格\t\t商品库存");
            for (Goods goods : goodsList) {
                System.out.println(""+goods.getGoodId()+"\t\t"+goods.getGoodName()+"\t\t"+goods.getPrice()+"\t\t"+goods.getNumber()+"");
            }
            String id = null;
            while (true) {
                // 2. 输入商品编号购买
                System.out.print("请输入商品编号：");
                 id = INPUT.next();

                // 判断goodsList是否有编号和id相同的商品
                boolean flag = false;
                for (Goods goods : goodsList) {
                    if(goods.getGoodId().equals(id)){
                        flag = true;
                        break;
                    }
                }
                if(!flag){
                    System.out.println("您输入的商品编号不存在");
                    continue;
                }
                else {
                    break;
                }
            }
            // 查询出集合中商品编号为id 的商品的库存
            Integer kuCun = 0;
            for (Goods goods : goodsList) {
                if(goods.getGoodId().equals(id)){
                    kuCun = goods.getNumber();
                }
            }
            int num = 0;
            while (true) {
                //请输入购买数量
                 num = INPUT.nextInt();
                if(num <= 0 || num > kuCun){
                    System.out.println("购买的数量不能为0或者负数也不能超过最大库存量！");
                    continue;
                }
                else {
                    break;
                }
            }
            // 提示
            System.out.println(" 您还要继续购买吗？(Y继续，其他取消购买)");
            String confirm = INPUT.next();

            switch (confirm){
                case "Y":
                    // 买
                    for (Goods goods : goodsList) {
                        if(goods.getGoodId().equals(id)){

                            // 将goodsList集合中该商品对象的库存减少 num
                            goods.setNumber(goods.getNumber()-num);

                            carList.add(goods);
                            System.out.println("成功添加商品到购物车");
                            continue outer ;
                        }
                    }
                    break;
                default:
                    // 取消购买
                    return;
            }
        }


    }
}
